package com.duoduo.springboot.shiro.service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.duoduo.springboot.shiro.dao.RoleDao;
import com.duoduo.springboot.shiro.model.Role;
import com.duoduo.springboot.shiro.vo.RoleVO;

@Service
public class RoleService {

	@Autowired
	private RoleDao roleDao;

	public List<RoleVO> listRolesByUserId(int userId) {
		List<RoleVO> result = new ArrayList<RoleVO>(0);
		List<Role> roleList = roleDao.listRolesByUserId(userId);
		if (!CollectionUtils.isEmpty(roleList)) {
			for (Role role : roleList) {
				result.add(RoleVO.fromEntity(role));
			}
		}
		return result;
	}

	public Set<String> listRoleNamesByUserId(int userId) {
		Set<String> result = new HashSet<String>(0);
		List<RoleVO> roles = listRolesByUserId(userId);
		if (!CollectionUtils.isEmpty(roles)) {
			for (RoleVO role : roles) {
				result.add(role.getName());
			}
		}
		return result;
	}
}
